//package com._4pd.demo.demo.crd;
//
//import com.alibaba.fastjson.JSON;
//import freemarker.template.Configuration;
//import freemarker.template.Template;
//import freemarker.template.Version;
//import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
//import io.fabric8.kubernetes.client.KubernetesClient;
//import io.fabric8.kubernetes.client.dsl.Resource;
//import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
//import io.javaoperatorsdk.operator.api.reconciler.*;
//import org.yaml.snakeyaml.Yaml;
//
//import java.io.File;
//import java.io.StringBufferInputStream;
//import java.io.StringWriter;
//import java.util.HashMap;
//import java.util.Map;
//
//@ControllerConfiguration
//public class AiOperatorReconciler implements Reconciler<AiOperator>{
//    private final KubernetesClient client;
//
//    public AiOperatorReconciler(KubernetesClient client){
//        this.client = client;
//    }
//    // Return the changed resource, so it gets updated. See javadoc for details.
//    @Override
//    public UpdateControl<AiOperator> reconcile(AiOperator aiOperator, Context<AiOperator> context) {
//        try {
//            AiOperatorSpec spec = aiOperator.getSpec();
//            String ai_type = spec.getAi_type();
//
//            switch (ai_type){
//                case "natong-simulate":
//                    Map<String,Object> data = new HashMap<>();
//                    data.put("run_conf", JSON.toJSONString(spec));
//                    data.put("name",aiOperator.getMetadata().getName());
//                    data.put("generateName",System.currentTimeMillis()+"");
//                    Configuration cfg = new Configuration(new Version("2.3.23"));
//                    cfg.setDirectoryForTemplateLoading(new File("src/main/resources"));
//                    cfg.setDefaultEncoding("UTF-8");
//                    Template template = cfg.getTemplate("freemarker/init-table.yaml");
//                    StringWriter out = new StringWriter();
//                    template.process(data, out);
//                    out.flush();
//                    String tmp = out.getBuffer().toString();
//                    System.out.println(tmp);
//
//                    final Yaml yaml = new Yaml();
//                    final Map<String, Object> obj = yaml.load(tmp);
//
//
//                    Resource<GenericKubernetesResource> cr = client.genericKubernetesResources(new ResourceDefinitionContext.Builder()
//                            .withKind("Workflow")
//                            .withGroup("argoproj.io")
//                            .withVersion("v1alpha1")
//                            .withNamespaced(true)
//                            .build()).load(new StringBufferInputStream(tmp));
//
//                    GenericKubernetesResource genericResource = cr.create();
//                    break;
//            }
//            UpdateControl.patchStatus(aiOperator);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}
//
